package com.tim.app.ui.adapter.selceted;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.net.StringResponseCallback;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.google.gson.Gson;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.api.API_User;
import com.tim.app.server.entry.ActionStateMsg;
import com.tim.app.server.entry.Topic;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.FollowResult;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.ui.activity.selected.TopicDetailActivity;
import com.tim.app.util.BitmapLoader;
import com.tim.app.util.ToastUtil;
import com.tim.app.util.ViewGT;

import java.util.List;


/**
 * 话题的适配器
 */
public class SelectSubjcetAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Topic> {

    static final String TAG = "SelectSubjcetAdapter";

    private Context mContext;

    public SelectSubjcetAdapter(Context mContext, List<Topic> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_select_subject_detail, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder mHolder, int position, final Topic data) {

        if (null == data)
            return;
        final ViewHolder holder = (ViewHolder) mHolder;
        if (!TextUtils.isEmpty(data.getImage())) {
            BitmapLoader.ins().loadImage(data.getImage(), holder.rivImage);
        } else {
            BitmapLoader.ins().loadImage("", holder.rivImage);
        }
        if (!TextUtils.isEmpty(data.getTopicName())) {
            holder.tvSubjectName.setText(data.getTopicName());
        }
        holder.tvFollowNumber.setText(mContext.getString(R.string.tvTotalFollowNumber, String.valueOf(data.getConcernCount())));
        if (null != data.getActionStateMsg()) {
            if (null != data.getActionStateMsg().getContentState()) {
                holder.tvFollow.setSelected(true);
                holder.tvFollow.setText(R.string.hasFollow);
            } else {
                holder.tvFollow.setSelected(false);
                holder.tvFollow.setText(R.string.unFollow);
            }
        }
        holder.rlContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TopicDetailActivity.start(mContext, data, false);
            }
        });
        holder.tvFollow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!UserManager.ins().isLogin()) {
                    ViewGT.gotoLoginActivity(mContext);
                    return;
                }
                holder.tvFollow.setEnabled(false);
                if (null != data.getActionStateMsg() && data.getActionStateMsg().getContentState() != null) {
                    int id = data.getActionStateMsg().getContentState();
                    API_User.ins().cancelFollow(TAG, id, UserManager.ins().getUserId(), data.getId(), AppKey.TYPE_TOPIC, new StringResponseCallback() {
                        @Override
                        public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                            holder.tvFollow.setEnabled(true);
                            if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                                holder.tvFollow.setSelected(false);
                                holder.tvFollow.setText(R.string.unFollow);
                                data.setConcernCount(data.getConcernCount() - 1);
                                holder.tvFollowNumber.setText(mContext.getString(R.string.tvTotalFollowNumber, String.valueOf(data.getConcernCount())));
                                data.getActionStateMsg().setContentState(null);
                            } else {
                                ToastUtil.showToast(errMsg);
                            }
                            return false;
                        }
                    });
                } else {
                    API_User.ins().addFollow(TAG, UserManager.ins().getUserId(), data.getId(), AppKey.TYPE_TOPIC, new StringResponseCallback() {
                        @Override
                        public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                            holder.tvFollow.setEnabled(true);
                            if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                                FollowResult result = new Gson().fromJson(resultValue, FollowResult.class);
                                if (null != result && null != result.getRes()) {
                                    if ( data.getActionStateMsg() == null) {
                                        data.setActionStateMsg(new ActionStateMsg());
                                    }
                                    data.getActionStateMsg().setContentState(result.getRes().getConcern().getId());
                                    holder.tvFollow.setSelected(true);
                                    data.setConcernCount(data.getConcernCount() + 1);
                                    holder.tvFollowNumber.setText(mContext.getString(R.string.tvTotalFollowNumber, String.valueOf(data.getConcernCount())));
                                    holder.tvFollow.setText(R.string.hasFollow);
                                }
                            } else {
                                ToastUtil.showToast(errMsg);
                            }
                            return false;
                        }
                    });
                }
            }
        });
    }


    public class ViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivImage;
        TextView tvSubjectName;
        TextView tvFollowNumber;
        TextView tvFollow;
        RelativeLayout rlContainer;

        public ViewHolder(View itemView) {
            super(itemView);
            rivImage = (RoundedImageView) itemView.findViewById(R.id.rivImage);
            tvSubjectName = (TextView) itemView.findViewById(R.id.tvSubjectName);
            tvFollowNumber = (TextView) itemView.findViewById(R.id.tvFollowNumber);
            tvFollow = (TextView) itemView.findViewById(R.id.tvFollow);
            rlContainer = (RelativeLayout) itemView.findViewById(R.id.rlContainer);
        }

    }

}
